package com.ldxiwxr.common.common;

import com.ldxiwxr.common.constant.CommonConstant;
import com.ldxiwxr.common.constant.HttpStatus;
import lombok.ToString;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author SuBanxia
 */

@ToString
public class Result<T>  implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final int SUCCESS = HttpStatus.SUCCESS;

    /** 失败 */
    public static final int FAIL = HttpStatus.FAIL;

    private int code;

    private String msg;

    private T data;

    public Result() {};

    public Result(T data, int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
        //this.data = data;
    }


    public static <T> Result<T> success()
    {
        return new Result(null, SUCCESS, null);
    }

    public static <T> Result<T> success(String msg)
    {
        return new Result(null, SUCCESS, msg);
    }

    public static <T> Result<T> data(T data)
    {
        return new Result(data, SUCCESS, null);
    }

    public static <T> Result<T> data(T data, String msg)
    {
        return new Result(data, SUCCESS, msg);
    }



    public static <T> Result<T> fail(String msg)
    {
        return new Result(FAIL, msg);
    }

    public static <T> Result<T> fail(T data)
    {
        return new Result(data, FAIL, null);
    }

    public static <T> Result<T> fail(T data, String msg)
    {
        return new Result(data, FAIL, msg);
    }

    public static <T> Result<T> fail(int code, String msg)
    {
        return new Result(null, code, msg);
    }

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }

    public Boolean isError()
    {
        return !isSuccess();
    }

    public Boolean isSuccess()
    {
        return Result.SUCCESS == getCode();
    }
}

